package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.events.commonEvent.RotationRelatedEvent;
	import com.tongji.housediy.model.SelectedObjectModel;
	import com.tongji.housediy.utils.MathCalculation;
	import com.tongji.housediy.view.components.NormalDoor;
	import com.tongji.housediy.view.components.RoomBorder;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class DoorRotationWindowMediator extends Mediator
	{
		[Inject]
		public var view:DoorRotationTitleWindow;
		///////////////////////by zazisb
		[Inject]
		public var selectedModel:SelectedObjectModel;
		//////////////////////////////////////////////
		
		///////////////////////////////////////////by zazisb 按钮调整门的翻转
		override public function onRegister():void
		{
			view.closeBtn.addEventListener(MouseEvent.CLICK, closeWindowHandler);
			
			view.leftRightBtn.addEventListener(MouseEvent.CLICK, leftRightBtnHandler);
			view.inOutBtn.addEventListener(MouseEvent.CLICK, inOutBtnHandler);
		}
		
		private function leftRightBtnHandler(event:MouseEvent):void
		{	
			if (selectedModel.selectedObj is NormalDoor)
			{
				var selectedDoor:NormalDoor = selectedModel.selectedObj as NormalDoor;
				var segPoint:Point = MathCalculation.getOffsetValue(((selectedDoor.parent) as RoomBorder).k, selectedDoor.length, ((selectedDoor.parent) as RoomBorder).data);
				var rotate:Number = selectedDoor.getReverseRotation(selectedDoor.data, ((selectedDoor.parent) as RoomBorder).k);
				var segFlag:Boolean = false;
				
				selectedDoor.dispatchEvent(new RotationRelatedEvent(RotationRelatedEvent.HIDE_ROTATION_BTN));
				
				switch(selectedDoor.rotateState)
				{
					case 1:
						selectedDoor.rotateState = 4;
						segFlag = false;
						break;
					
					case 2:
						selectedDoor.rotateState = 3;
						segFlag = false;
						break;
					
					case 3:
						selectedDoor.rotateState = 2;
						segFlag = true;
						break;
					
					case 4:
						selectedDoor.rotateState = 1;
						segFlag = true;
						break;
				}
				
				selectedDoor.transformNormalDoorState(segPoint, rotate, selectedDoor.rotateState, segFlag);
				selectedDoor.showRotationBtn();
			}
		}
		
		private function inOutBtnHandler(event:MouseEvent):void
		{
			if (selectedModel.selectedObj is NormalDoor)
			{
				var selectedDoor:NormalDoor = selectedModel.selectedObj as NormalDoor;
				var segPoint:Point = MathCalculation.getOffsetValue(((selectedDoor.parent) as RoomBorder).k, selectedDoor.length, ((selectedDoor.parent) as RoomBorder).data);
				var rotate:Number = selectedDoor.getReverseRotation(selectedDoor.data, ((selectedDoor.parent) as RoomBorder).k);
				var segFlag:Boolean = false;
				
				selectedDoor.dispatchEvent(new RotationRelatedEvent(RotationRelatedEvent.HIDE_ROTATION_BTN));
				
				switch(selectedDoor.rotateState)
				{
					case 1:
						selectedDoor.rotateState = 2;
						segFlag = false;
						break;
					
					case 2:
						selectedDoor.rotateState = 1;
						segFlag = false;
						break;
					
					case 3:
						selectedDoor.rotateState = 4;
						segFlag = true;
						break;
					
					case 4:
						selectedDoor.rotateState = 3;
						segFlag = true;
						break;
				}
				
				selectedDoor.transformNormalDoorState(segPoint, rotate, selectedDoor.rotateState, segFlag);
				selectedDoor.showRotationBtn();
			}
		}
		//////////////////////////////////////////////////////////////////////////////
		private function closeWindowHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
		}
	}
}